using Constriction.Walls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Constriction.Constrict;
using System;
using Constriction.Particle;

namespace Constriction
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        public const int SCREEN_WIDTH = 800;
        public const int SCREEN_HEIGHT = 600;
        public const int WALL_SIZE = 16;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public static Texture2D emptyTex;
        public static SpriteFont hudFont;
        public static Random rand;

        FogParticleSystem fogParticleSystem;
        WallManager wallManager;
        ConstrictionManager constrictionManager;
        Player player;
        Goal goal;
        ScoreHUD hud;

        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = SCREEN_WIDTH;
            graphics.PreferredBackBufferHeight = SCREEN_HEIGHT;
        }

        protected override void Initialize()
        {
            rand = new Random();

            player = new Player();
            goal = new Goal();
            wallManager = new WallManager();
            constrictionManager = new ConstrictionManager();

            player.wallManager = wallManager;

            hud = new ScoreHUD();

            fogParticleSystem = new FogParticleSystem(this);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            emptyTex = Content.Load<Texture2D>("Sprite/Empty");
            hudFont = Content.Load<SpriteFont>("Font/HUDFont");

            player.tex = Content.Load<Texture2D>("Sprite/Player");
            goal.tex = Content.Load<Texture2D>("Sprite/Goal");

            fogParticleSystem.Initialize(GraphicsDevice, Content, spriteBatch, constrictionManager);
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            HandleInput();
            player.Update(gameTime);
            constrictionManager.Update(gameTime);
            fogParticleSystem.Update(gameTime, constrictionManager);

            if (player.boundedBox.Intersects(goal.boundedBox))
            {
                HandleGoalCollision();
            }

            if (constrictionManager.DoesContactPlayer(player.boundedBox))
            {
                //TODO Hurt Player
            }

            base.Update(gameTime);
        }

        private void HandleInput()
        {
            var state = Keyboard.GetState();

            player.upPressed = state.IsKeyDown(Keys.Up);
            player.downPressed = state.IsKeyDown(Keys.Down);
            player.leftPressed = state.IsKeyDown(Keys.Left);
            player.rightPressed = state.IsKeyDown(Keys.Right);

            player.runPressed = state.IsKeyDown(Keys.LeftShift);
            player.walkPressed = state.IsKeyDown(Keys.LeftControl);
        }

        private void HandleGoalCollision()
        {
            hud.curScore += 1;
            goal.ChangePosition();
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            spriteBatch.Begin();

            constrictionManager.Draw(spriteBatch);
            player.Draw(spriteBatch);
            goal.Draw(spriteBatch);
            wallManager.Draw(spriteBatch);
            hud.Draw(spriteBatch);
            fogParticleSystem.Draw();

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}